package com.yanqu.road.server.cmd.activity.lanruosi;

import com.yanqu.road.entity.activity.lanruosi.LanRuoSiConfig;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiUserData;
import com.yanqu.road.pb.activity.LanRuoSiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lanruosi.LanRuoSiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lanruosi.LanRuoSiMgr;
import com.yanqu.road.server.pb.LanRuoSiPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_LANRUOSI_GET_GAME_DATA, desc = "获取游戏数据")
public class LanRuoSiGetGameDataCmd extends AbstractCommand {

    private int multi;

    private LanRuoSiProto.LanRuoSiGameDataMsg.Builder gameData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        LanRuoSiProto.LanRuoSiGetGameDataReqMsg reqMsg = LanRuoSiProto.LanRuoSiGetGameDataReqMsg.parseFrom(packet.getBytes());
        multi = reqMsg.getMulti();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        LanRuoSiConfig config = LanRuoSiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_OPEN;
            return;
        }

        // 旧配置的活动不能用16倍
        if (multi == 16 && config.getFastUnlockVipLevel() == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK_MULTI;
            return;
        }

        LanRuoSiModule module = player.getModule(LanRuoSiModule.class);
        LanRuoSiUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK;
            return;
        }

        gameData = LanRuoSiPb.parseGameDataMsg(userData, multi);
    }

    @Override
    public void sendData(GamePlayer player) {
        LanRuoSiProto.LanRuoSiGetGameDataRespMsg.Builder builder = LanRuoSiProto.LanRuoSiGetGameDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setGameData(gameData);
        }
        player.sendPacket(Protocol.U_LANRUOSI_GET_GAME_DATA, builder);
    }
}
